package org.example.view;

import org.example.entity.Movie;
import org.example.entity.MovieType;
import org.example.model.CinemaTableModel;

import javax.swing.*;
import java.awt.*;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.text.ParseException;
import java.text.SimpleDateFormat;

/**
 * @author TonySong
 * @date 2025/8/6 0006
 * @time 10:33
 */
public class MovieFrame extends JFrame {
    private Movie movie;

    {
        JLabel labName = new JLabel("电影名称");
        JLabel labDirector =new JLabel("导演");
        JLabel lalActor = new JLabel("主演");
        JLabel labType = new JLabel("电影类型");
        JLabel labPrice = new JLabel("票价");
        JLabel labPlayTime = new JLabel("放映时间");
        JTextField txtName = new JTextField(10);
        JTextField txtDirector = new JTextField(10);
        JTextField txtActor = new JTextField(10);
        JTextField txtType = new JTextField(10);
        JTextField txtPrice = new JTextField(10);
        JTextField txtPlayTime= new JTextField(10);
        //
        txtName.setEditable(false);
        txtDirector.setEditable(false);
        txtActor.setEditable(false);
        txtType.setEditable(false);
        txtPrice.setEditable(false);
        txtPlayTime.setEditable(false);
        //
        JButton btnBuy =new JButton("购买") ;
        btnBuy.addActionListener(e->{
           BuyTiaketFrmae.getInstance(movie);
        });
        JButton btnCancel=new JButton("取消");
        JTable table =new JTable(new CinemaTableModel());
        //表格选择行的事件
        table.addMouseListener(new MouseListener() {
            @Override
            public void mouseClicked(MouseEvent e) {
                // JOptionPane.showMessageDialog(null,"你选择了第"+table.getSelectedRow()+"行");
                int selectedRow = table.getSelectedRow();
                txtName.setText(table.getValueAt(selectedRow,0).toString());
                txtDirector.setText(table.getValueAt(selectedRow,1).toString());
                txtActor.setText(table.getValueAt(selectedRow,2).toString());
                txtType.setText(table.getValueAt(selectedRow,3).toString());
                txtPrice.setText(table.getValueAt(selectedRow,4).toString());
                txtPlayTime.setText(table.getValueAt(selectedRow,5).toString());

                movie=new Movie();
                movie.setActor(txtActor.getText());
                movie.setDirection(txtDirector.getText());
                movie.setName(txtName.getText());
                movie.setPrice(Integer.parseInt(txtPrice.getText()));
                movie.setType(MovieType.valueOf(txtType.getText()));
                try {
                    movie.setPlayTime(new SimpleDateFormat("dd:mm").parse(txtPlayTime.getText()));
                } catch (ParseException ex) {
                    throw new RuntimeException(ex);
                }
            }

            @Override
            public void mousePressed(MouseEvent e) {

            }

            @Override
            public void mouseReleased(MouseEvent e) {

            }

            @Override
            public void mouseEntered(MouseEvent e) {

            }

            @Override
            public void mouseExited(MouseEvent e) {

            }
        });
        JScrollPane jScrollPane=new JScrollPane(table);
        JPanel panel=new JPanel();
        JPanel panelSouth=new JPanel();

        panel.add(labName);panel.add(txtName);
        panel.add(labDirector);panel.add(txtDirector);
        panel.add(lalActor);panel.add(txtActor);
        panel.add(labType);panel.add(txtType);
        panel.add(labPrice);panel.add(txtPrice);
        panel.add(labPlayTime);panel.add(txtPlayTime);

        panelSouth.add(btnBuy);panel.add(btnCancel);
        this.setLayout(new BorderLayout());
        this.add(jScrollPane,BorderLayout.NORTH);
        this.add(panel,BorderLayout.CENTER);
        this.add(panelSouth,BorderLayout.SOUTH);
        this.setVisible(true);
        this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        this.setLocationRelativeTo(null);
    }

    public MovieFrame(){
        super("电影信息");
        this.pack();
    }
    public static void main(String[] args) {
        MovieFrame movieFrame = MovieFrame.MOVIE_FRAME;
    }

    public static final MovieFrame MOVIE_FRAME=new MovieFrame();

}
